<?php

class controller_friend
{
	public function __construct() {
		require BASEPATH.'model/model_friend.php';
		$this->Friend = new model_friend();
	}

	public function add_attention() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		//$type = get_login_type( $_GET['protocal']);
		$inviter = $_GET['inviter'];
		$invitee = $_GET['invitee'];
		$result->invitee = $invitee;
		$result->is_new = 1;

		require(BASEPATH.'library/date_helper.php');
		$time = strftime ( "%Y-%m-%d %H:%M:%S", now() );
		if (!$this->Friend->add_attention($inviter, $invitee, $time )) {
			$result->return_code = 10011;
		}
		else {
			$query = $this->Friend->get_attention($inviter, $invitee);
			if (query != null) {
				while($row = mysql_fetch_object($query)) {
					$result->user_list[] = $row;
				}
			}
		}
		label_return:
		echo json_encode($result);
	}
	public function add_attention_by_username()
	{
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		//$type = get_login_type( $_GET['protocal']);
		$inviter = $_GET['inviter'];
		$invitee_name = $_POST['invitee_name'];

		$user = $this->Friend->get_user_by_name( $invitee_name );
		if ($user == null) {
			$result->return_code = 10010;
			goto label_return;
		}
		if ($inviter == $user->userid) {
			$result->return_code = 10012;
			goto label_return;
		}
		$result->invitee = $user->userid;
		$result->is_new = 1;

		log_message('debug', "add_attention_by_username $user->userid   $user->username");
		require(BASEPATH.'library/date_helper.php');
		$time = strftime ( "%Y-%m-%d %H:%M:%S", now() );
		if (!$this->Friend->add_attention($inviter, $user->userid, $time)) {
			$result->return_code = 10011;
			goto label_return;
		}
		else {
			$query = $this->Friend->get_attention($inviter, $user->userid);
			while($row = mysql_fetch_object($query)) {
				$result->user_list[] = $row;
			}
		}
		label_return:
		echo json_encode($result);
	}
	public function remove_attention() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$inviter = $_GET['inviter'];
		$invitee = $_GET['invitee'];
		$result->invitee = $invitee;
		if (!$this->Friend->remove_attention($inviter, $invitee)) {
			$result->return_code = 1;
		}
		$result->invitee = $invitee;
		echo json_encode($result);
	}
	public function get_attention_list() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$result->is_new = 0;
		//$type = get_login_type( $_GET['protocal']);
		$inviter = $_GET['inviter'];
		$time = $_GET['time'];
		require(BASEPATH.'library/date_helper.php');
		$ftime = gmstrftime ( "%Y-%m-%d %H:%M:%S", $time);
		$query = $this->Friend->get_attention_list($inviter, $ftime);
		if ($query != null) {
			while($row = mysql_fetch_object($query)) {
				$result->user_list[] = $row;
			}
		}
		echo json_encode($result);
	}
	public function get_fans_list() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		//$type = get_login_type( $_GET['protocal']);
		$invitee = $_GET['invitee'];
		$time = $_GET['time'];
		require(BASEPATH.'library/date_helper.php');
		$ftime = gmstrftime ( "%Y-%m-%d %H:%M:%S", $time);
		$query = $this->Friend->get_fans_list($invitee, $ftime);
		if ($query != null) {
			while($row = mysql_fetch_object($query)) {
				$result->user_list[] = $row;
			}
		}
		echo json_encode($result);
	}
	public function update_user_dress() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		//$type = get_login_type( $_GET['protocal'] );
		$userid = $_GET['userid'];
		$plist = $_POST['plist'];
		$md5sum = $_POST['md5sum'];

		$username = $_POST['username'];
		$home = $_POST['home'];
		$level = $_POST['level'];
		$age = $_POST['age'];
		$star = $_POST['star'];
		$weight = $_POST['weight'];
		$height = $_POST['height'];

		require(BASEPATH.'library/date_helper.php');
		if (!$this->Friend->update_user_dress($userid, $username, $plist, $md5sum, $home, $level, $age, $star, $weight, $height, strftime ( "%Y-%m-%d %H:%M:%S", now ()))) {
			$result->return_code = 1;
		}
		echo json_encode($result);
	}
	public function modify_fans_state() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$result->inviter = $_GET['inviter'];
		$result->invitee = $_GET['invitee'];
		$result->flag = $_GET['flag'];

		$state = $this->Friend->get_friend_state($result->inviter, $result->invitee);
		if ($state == null) {
			$result->return_code = 10050;
			goto label_return;
		}
		if ($state == 2) {
			$result->return_code = 10051;
			goto label_return;
		}
		if ($result->flag != $state) {
			if (!$this->Friend->set_friend_state($result->inviter, $result->invitee, $result->flag) ) {
				$result->return_code = 1;
			}
		}
		label_return:
		echo json_encode($result);
	}
	public function update_introduction() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$userid = $_GET['userid'];
		$msg = $_POST['msg'];
		require(BASEPATH.'library/date_helper.php');
		$ftime = strftime ( "%Y-%m-%d %H:%M:%S", now() );
		if (!$this->Friend->update_introduction($userid, $msg, $ftime)) {
			$result->return_code = 1;
		}
		echo json_encode($result);
	}
	//粉丝拉票
	public function send_message() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$inviter = $_GET['inviter'];
		$type = $_GET['type'];
			
		require(BASEPATH.'library/date_helper.php');
		$ftime = strftime ( "%Y-%m-%d %H:%M:%S", now() );
		$this->Friend->add_message_to_all($inviter, $type, $ftime);
		
		label_return:
		echo json_encode($result);
	}
	// 	public function remove_message() {
	// 		$result = null;
	// 		$result->return_code = 0;
	// 		$result->protocal = get_protocal( $_GET ['protocal'] );
	// 		$result->msgid = $_GET['msgid'];
	// 		if ($this->Friend->remove_message($result->msgid)) {
	// 			$result->return_code = 1;
	// 		}
	// 		echo json_encode($result);
	// 	}
	public function request_attention() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );

		$inviter = $_GET['inviter'];
		$invitee = $_GET['invitee'];

		require(BASEPATH.'library/date_helper.php');
		$ftime = strftime ( "%Y-%m-%d %H:%M:%S", now() );
		if (!$this->Friend->add_message($inviter, $invitee, 5, $ftime)) {
			$result->return_code = 10014;
		}
		label_return:
		echo json_encode($result);
	}
	public function response_message() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );

		$result->msgid = $_GET['msgid'];
		$type = $_GET['type'];
		$inviter = $_GET['inviter'];
		$invitee = $_GET['invitee'];

		$response = $_GET['response'];

		log_message('debug', "response_message msgid $result->msgid   type $type   inviter $inviter  invitee  $invitee   response $response");

		if ($response == 1) {

			require(BASEPATH.'library/date_helper.php');
			$time = strftime ( "%Y-%m-%d %H:%M:%S", now() );
			if ($type == MSG_CG_REQUEST) {
				if ($this->Friend->cover_gril_vote($inviter, $time) ) {
					$this->Friend->add_message($invitee, $inviter, MSG_CG_RESPONSE, $time);
				}
			}
			else if ($type == MSG_PK_REQUEST) {
				require BASEPATH.'model/model_pk.php';
				$pk = new model_pk();
				if ($pk->pk_vote($inviter, $time_) ) {
					$this->Friend->add_message($invitee, $inviter, MSG_PK_RESPONSE, $time);
				}
			}
			else if ($type == MSG_CG_RESPONSE || $type == MSG_PK_RESPONSE) {
				$this->Friend->remove_message($result->msgid);
			}
			else if ($type == MSG_BOTH_FRIEND) {
				$result->is_new = 1;

				if (!$this->Friend->add_attention($invitee, $inviter, $time )) {
					$result->return_code = 10011;
				}
				else {
					$query = $this->Friend->get_attention($invitee, $inviter);
					if ($query != null) {
						while($row = mysql_fetch_object($query)) {
							$result->user_list[] = $row;
						}
					}
				}
			}
		}
		if ($result->return_code != 0 || $response == 0) {
			$this->Friend->remove_message($result->msgid);
		}
		label_return:
		echo json_encode($result);
	}
	public function get_message_list() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$receiverid = $_GET['receiverid'];
		$time = $_GET['time'];
		require(BASEPATH.'library/date_helper.php');
		$ftime = gmstrftime ( "%Y-%m-%d %H:%M:%S", $time);
		log_message('debug', 'get_message_list start time '.$time.'  '.$ftime);
		$query = $this->Friend->get_message_list($receiverid, $ftime);
		if ($query != null) {
			while ($row = mysql_fetch_object($query)) {
				$result->message_list[] = $row;
			}
		}
		echo json_encode($result);
	}
	// 	public function get_friend_status() {
	// 		$result = null;
	// 		$result->return_code = 0;
	// 		$result->protocal = get_protocal( $_GET ['protocal'] );
	// 		$userid = $_GET['userid'];
	// 		$time = $_GET['start_time'];
	// 		require(BASEPATH.'helpers/date_helper.php');
	// 		$ftime = gmstrftime ( "%Y-%m-%d %H:%M:%S", $time);
	// 		log_message('debug', 'get_friend_status start time '.$time.'  '.$ftime);

	// 	}
}

if (IS_MAINTAINING) {
	$result = null;
	$result->return_code = 10002;
	echo json_encode($result);
} else {
	$protocal = get_protocal( $_GET['protocal'] );
	log_message('debug', "enter protocal $protocal");

	$controller_friend = new controller_friend();
	switch ($protocal) {
		case 1201 :
			$controller_friend->add_attention_by_username();
			break;
		case 1202 :
			$controller_friend->add_attention();
			break;
		case 1203 :
			$controller_friend->get_attention_list();
			break;
		case 1204 :
			$controller_friend->get_fans_list();
			break;
		case 1205 :
			$controller_friend->update_user_dress();
			break;
		case 1206 :
			$controller_friend->remove_attention();
			break;
		case 1207 :
			$controller_friend->modify_fans_state();
			break;
		case 1208 :
			$controller_friend->update_introduction();
			break;
		case 1209 :
			$controller_friend->send_message();
			break;
		case 1210 :
			$controller_friend->remove_message();
			break;
		case 1211 :
			$controller_friend->get_message_list ();
			break;
		case 1212 :
			$controller_friend->add_attention_by_username();
			break;
		case 1213 :
			$controller_friend->get_friend_status();
			break;
		case 1214 :
			$controller_friend->response_message();
			break;
		case 1215 :
			$controller_friend->request_attention();
			break;
		default :
			break;
	}
}
